<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="公告名称" prop="noticeTitle">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入公告名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:notice:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="noticeList" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="100" />
      <el-table-column
        label="公告名称"
        align="center"
        prop="title"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="关联小区"
        align="center"
        prop="communityIds"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{ scope.row.communityOptions.join(",") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="发布时间"
        align="center"
        prop="createTime"
        width="100"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        width="100"
        :formatter="formatStatus"
      ></el-table-column>
      <el-table-column
        label="发布人"
        align="center"
        prop="createBy"
        width="100"
      />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <addEdit
      v-model="open"
      v-if="open"
      :title="title"
      :row="curRow"
      @submit="getList"
    />
  </div>
</template>

<script setup name="Notice">
import { removeNotice, getNoticeList } from "@/api/system/notice";
import { getCommunityPage } from "@/api/realEstateManage";
import addEdit from "./addEdit.vue";

const { proxy } = getCurrentInstance();

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");
const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  title: "",
  status: "",
});
const curRow = ref({});
/** 查询公告列表 */
function getList() {
  loading.value = true;
  getNoticeList(queryParams.value).then(({ data }) => {
    data.records.forEach((item) => {
      item.communityOptions = item.communityIds.map((id) => {
        const community = communityList.value.find((c) => c.id === id) || {};
        return community.name;
      });
    });
    noticeList.value = data.records;
    total.value = data.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNo = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  curRow.value = {};
  open.value = true;
  title.value = "添加公告";
}

/**修改按钮操作 */
function handleUpdate(row) {
  curRow.value = row;
  open.value = true;
  title.value = "修改公告";
}

/** 删除按钮操作 */
function handleDelete(row) {
  const noticeId = row.id;
  proxy.$modal
    .confirm("是否确认删除公告？")
    .then(function () {
      return removeNotice(noticeId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
function formatStatus(row, column, cellValue) {
  return row.status === 0 ? "草稿" : "发布";
}
// 获取小区列表
const communityList = ref([]);
const getCommunityList = () => {
  getCommunityPage({ pageNo: 1, pageSize: 9999 }).then(({ data }) => {
    communityList.value = data.records;
    getList();
  });
};
getCommunityList();
</script>
